import org.openjdk.jol.info.ClassLayout;

public class T15
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread.sleep(5000);

        Object lockO = new Object();
        synchronized (lockO) {
            // 1. 这是 偏向锁的持有
            System.out.println(ClassLayout.parseInstance(lockO).toPrintable());
            /**
             * java.lang.Object object internals:
             *  OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
             *       0     4        (object header)                           05 28 94 02 (00000101 00101000 10010100 00000010) (43264005)
             *       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
             *       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
             *      12     4        (loss due to the next object alignment)
             * Instance size: 16 bytes
             * Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
             */
        }

        // 2. 锁持有执行结束，然后进行释放。

        new Thread(() -> {
            synchronized (lockO) {
                // 3. 另一个线程 竞争锁资源，锁状态直接升级为 轻量级锁。
                System.out.println(ClassLayout.parseInstance(lockO).toPrintable());
                /**
                 * java.lang.Object object internals:
                 *  OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
                 *       0     4        (object header)                           10 f3 98 20 (00010000 11110011 10011000 00100000) (546894608)
                 *       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
                 *       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
                 *      12     4        (loss due to the next object alignment)
                 * Instance size: 16 bytes
                 * Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
                 */
            }
        }).start();
    }
}
